home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / -archivi / -recent2 / chunky_dev.lha / chunky_dev / Demos / Basics / BuffersAndDisplaying.c next >
C/C++ Source or Header  |  1999-03-16  |  11KB  |  355 lines

  1. //
  2. // chunky.library demo
  3. //
  4. // This example demonstrates how to use BitMap's as ChunkyPort's,
  5. // drawing parts of ChunkyPort's onto RastPort's and other ChunkyPort's.
  6. //
  7. // (c) 1999 Rosande Limited, all rights reserved.
  8. // PUBLIC DOMAIN
  9. //
  10. // http://www.irrelevant.org/~oondy/chunky/
  11.  
  12. #include <exec/types.h>
  13. #include <exec/libraries.h>
  14. #include <clib/chunky_protos.h>
  15. #include <libraries/chunky.h>
  16. #include <pragma/chunky_lib.h>
  17. #include <intuition/intuition.h>
  18. #include <pragma/intuition_lib.h>
  19. #include <pragma/graphics_lib.h>
  20. #include <pragma/exec_lib.h>
  21. #include <workbench/startup.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25.  
  26. #include "/_shared/loadcp.h"
  27. #include "/_shared/screen.h"
  28. #include "/_shared/waitbutton.h"
  29. #include "/_shared/requester.h"
  30.  
  31. struct Library *ChunkyBase;
  32. void *Colours = NULL;
  33. UBYTE *Bart = NULL;
  34.  
  35. #define BART_WIDTH  139
  36. #define BART_HEIGHT 232
  37.  
  38. void DoDemo(void);
  39. BOOL DemoAllocBitMap(void);
  40. BOOL DemoDrawChunky(void);
  41. BOOL DemoDrawChunkyChunky(void);
  42. BOOL DemoDrawChunkyWindow(void);
  43. void DemoDone(void);
  44.  
  45. void main2(void);
  46. void StatusText(STRPTR Text);
  47.  
  48. void wbmain(struct WBStartup *wbmsg)
  49. {
  50.   main2();
  51. }
  52.  
  53. main(int argc, char** argv)
  54. {
  55.   if(!argc) wbmain((struct WBStartup *)argv) else main2();
  56. }
  57.  
  58. void main2(void)
  59. {
  60.   // Open the library
  61.   if(ChunkyBase = OpenLibrary("chunky.library", 4))
  62.   {
  63.     // Open the screen
  64.     if(Colours = DEMO_LoadRGB32("/_shared/bart.col"))
  65.     {
  66.       if(DEMO_OpenScreen(640, 480, NULL, Colours))
  67.       {
  68.         CHK_ChooseHardwareMode(GetVPModeID(&DemoScreen->ViewPort));
  69.         if(Bart = DEMO_LoadChunky("/_shared/bart.chk", BART_WIDTH,
  70.          BART_HEIGHT))
  71.         {
  72.           DoDemo();
  73.         }
  74.         else printf("No bart.chk\n");
  75.       }
  76.       else printf("No memory for screen\n");
  77.     }
  78.     else printf("No bart.col\n");
  79.     CloseLibrary(ChunkyBase);
  80.   }
  81.   else printf("No chunky.library v4\n");
  82.  
  83.   if(Bart) DEMO_FreeChunky(Bart);
  84.   if(DemoScreen) DEMO_CloseScreen();
  85.   if(Colours) DEMO_FreeRGB32(Colours);
  86. }
  87.  
  88. void DoDemo(void)
  89. {
  90.   DEMO_Request("Welcome to the chunky.library Basic Demo!", "Woohoo!", NULL);
  91.   DEMO_Request("First off, let's create a BitMap using AllocBitMap()\n"
  92.                "and then convert it into a ChunkyPort.  I'll draw a\n"
  93.                "few rectangles on the BitMap in a few colours.", NULL, NULL);
  94.   if(!DemoAllocBitMap()) return;
  95.   if(!DemoDrawChunky()) return;
  96.   if(!DemoDrawChunkyChunky())  return;
  97.   if(!DemoDrawChunkyWindow()) return;
  98.   DemoDone();
  99. }
  100.  
  101. int GetRandNumber(int max)
  102. {
  103.   int n, ok = 0;
  104.   while(!ok)
  105.   {
  106.     n = rand();
  107.     if(!(n > max)) ok = 1;
  108.   }
  109.   return(n);
  110. }
  111.  
  112. void StatusText(STRPTR t)
  113. {
  114.   Move(DemoWindow->RPort, 20, 20);
  115.   if(t)
  116.    Text(DemoWindow->RPort, t, strlen(t))
  117.   else
  118.    SetRast(DemoWindow->RPort, 0);
  119. }
  120.  
  121. BOOL DemoAllocBitMap(void)
  122. {
  123.   struct BitMap *BitMap = NULL;
  124.   struct ChunkyPort *Converted = NULL;
  125.   struct RastPort rp;
  126.   BOOL ok = FALSE;
  127.  
  128.   // Allocate a bitmap
  129.   if(BitMap = AllocBitMap(320, 200, 8, BMF_CLEAR, NULL))
  130.   {
  131.     int x1, y1, x2, y2, c, i;
  132.     StatusText("Rendering boxes...");
  133.     InitRastPort(&rp); rp.BitMap = BitMap;
  134.     // Draw some rectangles on it
  135.     for(i = 0; i < 50; i++)
  136.     {
  137.       x1 = GetRandNumber(319); y1 = GetRandNumber(199);
  138.       x2 = x1 + GetRandNumber(319-x1); y2 = y1 + GetRandNumber(199-y1);
  139.       c = GetRandNumber(254)+1;
  140.       SetAPen(&rp, c);
  141.       RectFill(&rp, x1, y1, x2, y2);
  142.     }
  143.     StatusText(NULL);
  144.  
  145.     DEMO_Request("Okay, drawn the boxes.  Now let's convert the BitMap\n"
  146.                  "into a ChunkyPort...", NULL, NULL);
  147.  
  148.     if(Converted = CHK_CreateChunkyFromBitMap(BitMap, 0, 0, 320, 200))
  149.     {
  150.       DEMO_Request("... and now bring it to life by using CHK_DrawChunky()...", "Oh yes!", NULL);
  151.  
  152.       // Draw the converted BitMap onto the screen
  153.       CHK_DrawChunky(Converted, DemoWindow->RPort, 160, 140);
  154.  
  155.       DEMO_Request("And volia!  Doesn't that look pretty? :)", "No!", NULL);
  156.       ok = TRUE;
  157.     }
  158.     else
  159.     {
  160.       printf("Can't CHK_CreateChunkyFromBitMap() in DemoAllocBitMap()!\nNo memory?");
  161.     }
  162.   }
  163.   if(Converted) CHK_FreeChunky(Converted);
  164.   if(BitMap) FreeBitMap(BitMap);
  165.   return(ok);
  166. }
  167.  
  168. BOOL DemoDrawChunky(void)
  169. {
  170.   struct ChunkyPort *cp = NULL;
  171.   int x, y, i, ok = FALSE;
  172.  
  173.   DEMO_Request("Now, boxes are very nice, but let's use some real graphics.\n"
  174.                "Whilst you weren't looking, I loaded a chunky picture.  It is\n"
  175.                "just a chunky image at the moment, and chunky.library doesn't\n"
  176.                "know about it.  But, let's make it so it does.\n\n"
  177.                "We do this allocating a buffer...", NULL, NULL);
  178.   if(cp = CHK_InitChunky(BART_WIDTH, BART_HEIGHT))
  179.   {
  180.     DEMO_Request("...and then by CopyMem()'ing the chunky buffer into the\n"
  181.                  "ChunkyPort I just made...", "Wooo...",  NULL);
  182.     //
  183.     // Okay this is a bit of a hack, but seems the most sensible way of
  184.     // doing it.  Perhaps V5 will have a command to copy the data across..
  185.     // :)
  186.     //
  187.     CopyMem(Bart, cp->cp_Chunky, (BART_WIDTH * BART_HEIGHT));
  188.  
  189.     DEMO_Request("I must stress that\m The Simpsons is (C) & (TM)\n"
  190.                  "20th Century Fox Film Corporation, all rights reserved\n"
  191.                  "This image is not used with permission.\n\n"
  192.                  "Bah, it's for educational purposes damnit! :)", "Right on!", NULL);
  193.  
  194.     CHK_DrawChunky(cp, DemoWindow->RPort, (640-BART_WIDTH)/2, (480-BART_HEIGHT)/2);
  195.  
  196.     DEMO_Request("Don't have a cow, man!", "Eat my shorts!", NULL);
  197.     DEMO_Request("Okay, let's have lots of Barts...", NULL, NULL);
  198.  
  199.     // There is a reason for this - demonstrating CHK_InsertChunky() in a mo :)
  200.     for(i = 0; i < 5; i++)
  201.     {
  202.       x = GetRandNumber(640-BART_WIDTH); y = GetRandNumber(480-BART_HEIGHT);
  203.       CHK_DrawChunky(cp, DemoWindow->RPort, x, y);
  204.     }
  205.  
  206.     DEMO_Request("Hang on a minute.  CHK_DrawChunky() has also drawn colour zero\n"
  207.                  "too, but I wanted it to be drawn transparent!\n\n"
  208.                  "No problemo, CHK_InsertChunky() to the rescue!", "Hooray!", NULL);
  209.  
  210.     StatusText(NULL);
  211.     for(i = 0; i < 5; i++)
  212.     {
  213.       x = GetRandNumber(640-BART_WIDTH); y = GetRandNumber(480-BART_HEIGHT);
  214.       CHK_InsertChunky(cp, DemoWindow->RPort, x, y);
  215.     }
  216.  
  217.     DEMO_Request("Cool!\n\nYou'll note that CHK_InsertChunky() can be\n"
  218.                  "slow.  Blame ReadPixelArray8() for that.", "Dang", NULL);
  219.  
  220.     CHK_FreeChunky(cp);
  221.     ok = TRUE;
  222.   }
  223.   else
  224.   {
  225.     printf("Can't alloc mem for Bart!\n");
  226.   }
  227.   return(ok);
  228. }
  229.  
  230. void WriteLabel(STRPTR t, UWORD x, UWORD y)
  231. {
  232.   SetAPen(DemoWindow->RPort, 1);
  233.   Move(DemoWindow->RPort, x, y); Text(DemoWindow->RPort, t, strlen(t));
  234. }
  235.  
  236. BOOL DemoDrawChunkyChunky(void)
  237. {
  238.   struct ChunkyPort *cp = NULL, *cp2 = NULL;
  239.   BOOL ok = FALSE;
  240.  
  241.   if((cp = CHK_InitChunky(128, 128)) && (cp2 = CHK_InitChunky(128, 128)))
  242.   {
  243.     DEMO_Request("Now, let's demonstrate drawing ChunkyPort's inside ChunkyPort's.\n\n"
  244.                  "To do this, I've allocated two identical ChunkyPort's.\n"
  245.                  "Let's draw a different coloured rectangle in each.", NULL, NULL);
  246.  
  247.     // Hell, I'm not supposed to demonstrate CHK_RectFill() here, but
  248.     // never mind :)
  249.     StatusText("CHK_RectFill()");
  250.     CHK_SetAPen(cp, 2);  CHK_SetAPen(cp2, 3);
  251.     CHK_RectFill(cp, 0, 0, 128, 128);
  252.     CHK_RectFill(cp2, 32, 32, (32+64), (32+64));
  253.     StatusText(NULL);
  254.     CHK_DrawChunky(cp, DemoWindow->RPort, 20, 200);
  255.     CHK_DrawChunky(cp2, DemoWindow->RPort, (20+cp->cp_Width)+16, 200);
  256.     WriteLabel("ChunkyPort 1", 32, 188);
  257.     WriteLabel("ChunkyPort 2", (32+cp->cp_Width), 188);
  258.  
  259.     DEMO_Request("Right, there's two ChunkyPort's drawn on the window using\n"
  260.                  "CHK_DrawChunky().  Remember that both ChunkyPort's are the\n"
  261.                  "same width,height - I've drawn the rectangle on ChunkyPort 2\n"
  262.                  "to be half the size as the rectangle on ChunkyPort 1.\n\n"
  263.                  "What I'll do now is CHK_DrawChunkyChunky() CP 2 onto CP 1...",
  264.                  "Groovy", NULL);
  265.  
  266.     CHK_SetDrMd(cp2, JAM1); // new in v4
  267.     CHK_DrawChunkyChunky(cp, cp2, 0, 0);
  268.     // Draw the rectangle-on-rectangle onto  the window
  269.     CHK_DrawChunky(cp, DemoWindow->RPort, 20, 200);
  270.     // Just to iterate that cp2 is the same  as before. :)
  271.     CHK_DrawChunky(cp2, DemoWindow->RPort, (20+cp->cp_Width)+16, 200);
  272.  
  273.     DEMO_Request("Tada!", NULL, NULL);
  274.     StatusText(NULL);
  275.     ok =  TRUE;
  276.     CHK_FreeChunky(cp); CHK_FreeChunky(cp2);
  277.   }
  278.   else
  279.   {
  280.     printf("DemoDrawChunkyChunky() has no mem!\n");
  281.   }
  282.   return(ok);
  283. }
  284.  
  285. BOOL DemoDrawChunkyWindow(void)
  286. {
  287.   struct Window *win = NULL;
  288.   struct ChunkyPort *cp = NULL;
  289.   BOOL ok = FALSE;
  290.  
  291.   DEMO_Request("Okay, new in V4 of chunky.library is a command called\n"
  292.                "CHK_DrawChunkyWindow().  You'll know that all the drawing\n"
  293.                "commands use RastPort's to display their data.  That's okay,\n"
  294.                "but it's not very friendly if you're using a proper\n"
  295.                "windowed environment.  CHK_DrawChunkyWindow() takes into\n"
  296.                "account the Window's borders, and renders a ChunkyPort\n"
  297.                "without overwriting the borders, etc.  So to demonstrate...",
  298.                "Open a Window!", NULL);
  299.  
  300.   // First of all, we gonna reuse Bart for this...
  301.   if(cp = CHK_InitChunky(BART_WIDTH, BART_HEIGHT))
  302.   {
  303.     CopyMem(Bart, cp->cp_Chunky, BART_WIDTH*BART_HEIGHT);
  304.  
  305.     if(win = OpenWindowTags(NULL,
  306.        WA_Left, 128,
  307.        WA_Top,  128,
  308.        WA_Width, BART_WIDTH+60,
  309.        WA_Height, BART_HEIGHT+20,
  310.        WA_Flags, WFLG_DRAGBAR|WFLG_ACTIVATE,
  311.        WA_CustomScreen, DemoScreen,
  312.        WA_Title, "Will drop pants for food",
  313.        TAG_DONE))
  314.     {
  315.       DEMO_Request("Drawing Bart using CHK_DrawChunky(cp, win->RPort, 0, 0)", "Right", NULL);
  316.  
  317.       CHK_DrawChunky(cp, win->RPort, 0, 0);
  318.  
  319.       DEMO_Request("See, went over the borders... let's refresh the window\n"
  320.                    "and draw with CHK_DrawChunkyWindow(cp, win, 0, 0)...", "Oooh", NULL);
  321.  
  322.       SetRast(win->RPort, 0); RefreshWindowFrame(win);
  323.       // Autodocs for this function were wrong prior to V4.1
  324.       CHK_DrawChunkyWindow(cp, win, 0, 0);
  325.  
  326.       DEMO_Request("Wow, what a difference :)", "Heh", NULL);
  327.  
  328.       ok = TRUE;
  329.     }
  330.     else
  331.     {
  332.       printf("OpenWindowTags() failed in DemoDrawChunkyWindow()\n");
  333.     }
  334.     CHK_FreeChunky(cp);
  335.   }
  336.   else
  337.   {
  338.     printf("No mem in DemoDrawChunkyWindow()\n");
  339.   }
  340.   if(win) CloseWindow(win);
  341.   return(ok);
  342. }
  343.  
  344. void DemoDone(void)
  345. {
  346.   StatusText(NULL);
  347.   WriteLabel("And that's it for this Basic demo!", 20, 20);
  348.   WriteLabel("We hope you had a lot of fun, and will tune in next time", 20, 30);
  349.   WriteLabel("for another thrilling installment of the Cosby Show.", 20, 40);
  350.   WriteLabel("Same Bat-time, same Bat-channel!", 20, 60);
  351.   WriteLabel("D'oh!", 20, 120);
  352.   WriteLabel("Click the close button in the top-left to exit this madness.", 20, 140);
  353.   DEMO_WaitForCloseGadget();
  354. }
  355.